<?
/**
 *票务通订单类.
 *主要用于票票付通平台会员的订单统计，查询操作
 */
class ProOrder
{
    /**
     *票付通内部接口
     *@var object
     */
    public $s;

    /**
     *登陆会员ID
     *@var int
     */
    public $did;

    /**
     *总订单数
     *@var int 
     */
    //public $totalOrders;

    /**
     *订单查询参数,键值数组
     *@var array 
     */
    public $params;

    /**
     *订单查询初始参数，键值数组
     *@var array 
     */
    private $initParam;

    /**
     *构造函数.
     *会初始化初始查询参数 initParam（默认权限全开）
     *@param class $s 票付通内部接口 
     *@param class $did 会员表ID 
     */
    public function __construct($did, $s)
    {
        $this->s = $s;
        $this->did = $did;
        $this->initParam = $this->params = array(
            "status" => "",
            "pays" => "",
            "orderby" => 1,
            "sort" => 1,
            "rstart" => 0,
            "n" => 99999999,//销售报表默认使用该参数。
            "c" => 0,
            "stype" => 0,
            "payinfo" => "",
            "pmode" => 0,
            "remotenum" => "",
            "origin" => 0,
            "p_type" => "",
            "order_confirm" => "",
            "aid" => "",
            "concat" => 1,
            "ifpack" => 0);
    }

    /**
     *修改订单查询初始参数函数.
     *有查询权限的限制，必须调用该函数增加限制条件
     *@param string $key 对应查询接口参数名
     *@param string $value 对应$key参数的值
     *@return void
     */
    public function setInitParam($key, $value)
    {
        $this->initParam[$key] = $value;
    }

    /**
     *还原订单查询初始函数.
     *用于增加查询条件之后的参数还原
     *@return void
     */
    public function init()
    {
        $this->params = $this->initParam;
    }

    /**
     *订单查询参数处理.
     *接收来自页面$_REQUEST传递过来的查询参数请求处理，将查询条件添加到params里，会更改params的值
     *值涉及时间，排序，支付方式等不涉及用户查询权限的值，传入是mid和aid时需判定是否该初始限定已存在
     */
    public function handleRequest()
    {
		
        $tjs = array(
            0 => "ordernum",
            1 => "ltitle",
            2 => "oname",
            3 => "otel",
            6 => "remotenum");
        $btimes = array(
            1 => "btime1",
            2 => "btime2",
            3 => "btime3");
        $etimes = array(
            1 => "etime1",
            2 => "etime2",
            3 => "etime3");

        $text = trim($_REQUEST['txt']);
		
        $len = mb_strlen($text, 'utf-8');
        if(is_numeric($text) && $len==11 ) {
            $this->params['otel'] = $text;
        }
        elseif(is_numeric($text) && $len<11) {
            $this->params['ordernum'] = $text;
        }
        elseif(!is_numeric($text) && $len<5 && abs($_REQUEST['tjs']) == 2) {
			//取票人
            $this->params['oname'] = $text;
			
        }else{
			// echo "我来也";
            if (abs($_REQUEST['tjs']) == 4) {
                if (!$this->params['mid']) { //分销商初始条件不存在
                     $str = "select id from pft_member where dname='$text'";
					//$str = "SELECT id FROM pft_member WHERE dname LIKE '%$text%'";
                    $GLOBALS['le']->query($str);
                    $mids = array();
                    while ($row = $GLOBALS['le']->fetch_assoc()) {
                        $mids[] = $row['id'];
                        if (count($mids) > 9)
                            break;
                    }
					if (!$mids) $this->params['mid'] = -1;
					else {					
						$mids = implode("|", $mids);
						$this->params['mid'] = $mids;
					}
					$this->params['aid'] = $this->did;
                 
                }
            }
            elseif (abs($_REQUEST['tjs'] == 5)) { //搜索供应商
				// echo "我在这";
                if (!$this->params['aid']) { //供应商初始条件不存在
					$str = "select * from pft_member where dname='$text'";
                    //$str = "select * from pft_member where dname like '%$text%'";
                    $GLOBALS['le']->query($str);
                    $aids = array();
					// $arr = array();
                    while ($row = $GLOBALS['le']->fetch_assoc()) {
						// print_r($row);
						$arr[]= $row;
                        $aids[] = $row['id'];
                        if (count($aids) > 9)
                            break; //搜出的供应商太多只搜出前9个
                    }
					// print_r($arr);
					if (!$aids) $this->params['aid'] = -1;
                    else{
						$aids = implode("|", $aids);
						$this->params['aid'] = $aids;
					}
					$this->params['mid'] = $this->did;
                }
				// exit;
            }
            else {
                $this->params[$tjs[abs($_REQUEST['tjs'])]] = urldecode($text);
            }
        }
        if (isset($_REQUEST['payinfo'])) { //支付方式
            $this->params['payinfo'] = implode("|", $_REQUEST['payinfo']);
        }
        if ($_REQUEST['begintime'] || $_REQUEST['endtime']) { //查询时间段
            $this->params[$btimes[abs($_REQUEST['times'])]] = $_REQUEST['begintime'] .
                " 00:00:00";
            $this->params[$etimes[abs($_REQUEST['times'])]] = $_REQUEST['endtime'] .
                " 23:59:59";
        }
		
		//按时间查询订单
		if(isset($_REQUEST['begintime']) && $_REQUEST['begintime']){
			$etime = $_REQUEST['endtime'] ? $_REQUEST['endtime']:date("Y-m-d");
			$etime.=' 23:59:59';
			if($_REQUEST['xtimes']==1){
				$this->params['btime1'] = $_REQUEST['begintime'].' 00:00:00';
				$this->params['etime1'] = $etime;
			}elseif($_REQUEST['xtimes'] == 2){
				$this->params['btime2'] = $_REQUEST['begintime'].' 00:00:00';
				$this->params['etime2'] = $etime;
			}else{
				$this->params['btime3'] = $_REQUEST['begintime'].' 00:00:00';
				$this->params['etime3'] = $etime;
			}
		}

		
        if (isset($_REQUEST['orderby'])) { //排序
            $this->params['orderby'] = $_REQUEST['orderby'];
        }
        if (isset($_REQUEST['sort'])) { //升降序
            $this->params['sort'] = $_REQUEST['sort'];
        }
        if (isset($_REQUEST['status']) && $_REQUEST['status'] !== "") { //订单状态
            $this->params['status'] = $_REQUEST['status'];
        }
        if (trim($_REQUEST['ordernum'])) { //订单号
            $this->params['ordernum'] = trim($_REQUEST['ordernum']);
        }
		
		//查询订单时从0开始
		if($this->params['ordernum']){
			$this->params['rstart'] = 0;
		}
    }

    /**
     *返回订单总数
     *请确保调用过查询接口，否则该值为空
     *@return int
     */
    public function orderlistTotal()
    {
        return $this->totalOrders();
    }

    /**
     *订单查询分页函数.
     *联票订单按一条汇总显示
     *@param int currentPage 当前页
     *@param int pageSize  每页显示记录数
     *@return array
     */
    public function orderlist($currentPage = 1, $pageSize = 15)
    {

        $this->init();
        $this->handleRequest();
        $this->params['rstart'] = ($currentPage - 1) * $pageSize;
        $this->params['n'] = $pageSize;
		//员工查询需找到公司id
		if($_SESSION['dtype']==6 && isset($_SESSION['sid']))
			$this->params['mid'] = $_SESSION['sid'];
        $simplexml = $this->ordersSimpleXml();
        $result = array();
		$other = array();
        foreach ($simplexml->Rec as $row) {

            //处理转供应产品价格
            $row = $this->secondProOrderHandle($row);
			
            if ($this->params['ordernum']) { //直接查订单号，
                if ($this->params['ordernum'] != $row->UUordernum){
					$other = $row;
					continue;
				}
               // print_r($other);
			   // exit;
               $array= array(0 => $row);
               $result[] = OrderRecord::_format($array,$other);
            } else {
                if ($row->UUconcat_id == "main" || $row->UUifpack == 1) {
                    $or = new OrderRecord((string )$row->UUordernum, $this->s);
					$result[] = $or->info();
                } else {
				 
                    $array = array(0 => $row);
                    $result[] = OrderRecord::format($array);
                }
            }
			
        }
		//print_r($result);
        return $result;
    }
	/**
     *订单查询分页函数.
     *联票订单按一条汇总显示
     *@param int currentPage 当前页
     *@param int pageSize  每页显示记录数
     *@return array
	 *说明：为解决查询从订单一并列出主订单稍作改写；
     */
    public function _orderlist($currentPage = 1, $pageSize = 15)
    {

        $this->init();
        $this->handleRequest();
        $this->params['rstart'] = ($currentPage - 1) * $pageSize;
        $this->params['n'] = $pageSize;
        $simplexml = $this->ordersSimpleXml();

		//从订单有主订单时获取主订单订单号
		if($simplexml->Rec->UUconcat_id!=0 && $simplexml->Rec->UUconcat_id!='main'){
			$tmp = $simplexml->Rec;
			$this->params['ordernum'] = (string)$simplexml->Rec->UUconcat_id;
			$simplexml = $this->ordersSimpleXml();
		}
		// print_r($simplexml);
		// exit;
        $result = array();
		$other = array();
        foreach ($simplexml->Rec as $row) {
            //处理转供应产品价格
            $row = $this->secondProOrderHandle($row);
			
            if ($this->params['ordernum']) { //直接查订单号，

                if ($this->params['ordernum'] != $row->UUordernum){
					$other = $row;
					continue;
				}
				// 主订单出现在前  后是从订单情况
				if(isset($simplexml->Rec[$key+1]) && empty($other)){
					$other = $simplexml->Rec[$key+1];
				}
				
               $array= array(0 => $row);
			   // print_r($array);
			   // print_r($other);
			   // exit;
			   
               $result[] = OrderRecord::_format($array,$other);
            } else {
                if ($row->UUconcat_id == "main" || $row->UUifpack == 1) {
                    $or = new OrderRecord((string )$row->UUordernum, $this->s);
					$result[] = $or->info();
                } else {
                    $array = array(0 => $row);
                    $result[] = OrderRecord::format($array);
                }
            }
			
        }
        return $result;
    }
	
    /**
     *转供应产品订单记录处理函数
     *转供应产品价格处理，使多级价格能正确显示
     *@param int currentPage 当前页
     *@param int pageSize  每页显示记录数
     *@return array
     */
    public function secondProOrderHandle($row)
    {
			
        if ((string )$row->UUaids&&(string )$row->UUaids_price) { //是多级供应产品
			$UUtprices = array();
			$UUaids = explode(",", (string )$row->UUaids);
			$UUaids_price = explode(",", (string )$row->UUaids_price);
			/*转供应商家查已卖出产品*/
			if ($this->params['aid']) {
				
				//转供应价格 如UUaids:81,94 UUaids_price:4. 就是81供应价是4，94分销价是4
				for ($i = 0; $i < count($UUaids) - 1; $i++) {
					$UUtprices[$UUaids[$i]] = $UUaids_price[$i];
				}
				$UUtprices[(string)$row->UUaid]=(string)$row->UUtprice;
				$aids = explode("|", $this->params['aid']);
				
				//if($row->UUordernum=="103891"){print_r($row);print_r($UUtprices);}
				/*从最初供应商逐级查询，优先显示多级价*/
				foreach($UUtprices as $aid=>$tprice){
					if(in_array($aid,$aids)){
						$row->SecondHand = 1;
						$pos = array_search($aid,$UUaids);
						$row->UUmid = $UUaids[$pos + 1];
						$row->UUtprice = $tprice;
						$row->UUtotalmoney = $row->UUtprice * $row->UUtnum;
					}
				}
				
				
				/*
				foreach ($aids as $aid) {
					if (isset($UUtprices[$aid])) {
						
						$row->SecondHand = 1;
						$pos = array_search($aid,$UUaids);
						$row->UUmid = $UUaids[$pos + 1];
						$row->UUtprice = $UUtprices[$aid];
						$row->UUtotalmoney = $row->UUtprice * $row->UUtnum;
					}
				}*/
				
			}
			if ($this->params['mid']) {
				$UUmids=$UUaids;
				$UUmids[]=(string)$row->UUmid;
				$UUtprice[(string)$row->UUmid]=(string)$row->UUtprice;
				for ($i = 1; $i < count($UUaids); $i++) {
					$UUtprices[$UUaids[$i]] = $UUaids_price[$i - 1];
				}
				$mids = explode("|", $this->params['mid']);
				/*从最初分销商逐级查询，优先显示多级价*/
				foreach($UUtprices as $mid=>$tprice){
					if(in_array($mid,$mids)){
						$row->SecondHand = 1;
						$pos = array_search($mid,$UUmids);
						$row->UUaid = $UUmids[$pos - 1];
						$row->UUtprice =$tprice;
						$row->UUtotalmoney = $row->UUtprice * $row->UUtnum;
					}
				}
				/*
				foreach ($mids as $mid) {
					if (isset($UUtprices[$mid])) {
						$row->SecondHand = 1;
						$pos = array_search($mid, $UUaids);
						$row->UUaid = $UUaids[$pos - 1];
						$row->UUtprice = $UUtprices[$mid];
						$row->UUtotalmoney = $row->UUtprice * $row->UUtnum;
					}
				}*/
			}
		}
        return $row;
    }

    /**
     *订单当日汇总统计.
     *显示当天的订单汇总信息,在首页店铺提醒显示
     *@return array
     */
    public function dayOrdersCount()
    {
		$sum=array(0,0,0);
        $btime1 = date("Y-m-d 00:00:00");
        $etime1 = date("Y-m-d 23:59:59");
		$result=$this->orderSum($btime1, $etime1);
		foreach($result as $lid=>$row){
			$sum[0]+=$row['order'];
			$sum[1]+=$row['tnum'];
			$sum[2]+=$row['money'];
		}
		return $sum;
    }
	
	/**
     *指定日期的订单汇总统计.
     *显示当天的订单汇总信息,在首页店铺提醒显示
     *@return array
     */
	public function _dateOrdersCount($btime='',$etime=''){
		if(!$btime)
			$btime = date('Y-m-d');
		if(!$etime)
			$etime = date('Y-m-d');
		$sum=array(0,0,0);
        $btime1 = $btime.' 00:00:00';
        $etime1 = $etime.' 23:59:59';
		$result=$this->orderSum($btime1, $etime1);
		foreach($result as $lid=>$row){
			$sum[0]+=$row['order'];
			$sum[1]+=$row['tnum'];
			$sum[2]+=$row['money'];
		}
		return $sum;
	}
    /**
     *7日订单销售统计函数.
     *显示7天的订单报表汇总信息
     *@return array
     */
    public function weekOrders()
    {
        $begintime = time() - 24 * 3600 * 7;
        $btime1 = date("Y-m-d 00:00:00", $begintime);
        $etime1 = date("Y-m-d 23:59:59");
        return $this->orderSum($btime1, $etime1);
    }

    /**
     * 订单销售统计函数.用于报表查询统计
     *
     * @param $begintime 开始时间
     * @param $endtime 结束时间
     * @param string $by 统计方式:lid按景区,tid按门票,mid按分销商
     * @param array $option 查询参数
     * @param int $timeType 时间类型1下单时间2使用时间3游玩时间(cgp, 2014年1月24日 13:52:18)
     * @param int $fid   分销商ID
     * @return array
     */
    public function orderSum($begintime, $endtime, $by = "lid",
                             $option = array(), $timeType=1, $fid=0)
    {
        $this->init();
        $btime1 = $begintime;
        $etime1 = $endtime;
        $this->params['concat'] = 0;
        if($fid>0) {
            $this->params['mid'] = $fid;
        }
        switch($timeType) {
            case 2:
                $this->params['btime2'] = $btime1;
                $this->params['etime2'] = $etime1;
                break;
            case 3:
                $this->params['btime3'] = $btime1;
                $this->params['etime3'] = $etime1;
                break;
            default:
                $this->params['btime1'] = $btime1;
                $this->params['etime1'] = $etime1;
                break;
        }
        foreach ($option as $key => $value) {
            $this->params[$key] = $value;
        }
        $simplexml = $this->ordersSimpleXml();
        $result = array();
        $UUby = "UU" . $by;
        foreach ($simplexml->Rec as $row) {
			// print_r($row);
            if ($row->UUpaystatus != 1)
                continue; //未支付订单不统计
            $row = $this->secondProOrderHandle($row); //转供应产品价格处理
            if ($by == "lid") {
                $result[(string )$row->{$UUby}]['title'] = (string )$row->UUltitle;
            } elseif ($by == "tid") {
                $result[(string )$row->{$UUby}]['title'] = (string )$row->UUltitle . (string )$row->
                    UUttitle;
            } else {
                $result[(string )$row->{$UUby}]['title'] = (string )$row->UUdname;
            }
            $result[(string )$row->{$UUby}]['order']++;
            $result[(string )$row->{$UUby}]['tnum'] += $row->UUtnum;
            $result[(string )$row->{$UUby}]['money'] += (float)$row->UUtnum*(float)$row->UUtprice;
            if ((string )$row->UUpmode == 0)
                $result[(string )$row->{$UUby}]['pmode0'] += (float)$row->UUtnum * (float)$row->
                    UUtprice;
            if ((string )$row->UUpmode == 1)
                $result[(string )$row->{$UUby}]['pmode1'] += (float)$row->UUtnum * (float)$row->
                    UUtprice;
            if ((string )$row->UUpmode == 2)
                $result[(string )$row->{$UUby}]['pmode2'] += (float)$row->UUtnum * (float)$row->
                    UUtprice;
            if ((string )$row->UUpmode == 3)
                $result[(string )$row->{$UUby}]['pmode3'] += (float)$row->UUtnum * (float)$row->
                    UUtprice;
        }

        return $result;
    }
	
	
	 /**
	 * 订单销售统计函数.用于报表查询统计
	 *
	 * @param $begintime 开始时间
	 * @param $endtime 结束时间
	 * @param string $by 统计方式:lid按景区,tid按门票,mid按分销商
	 * @param array $option 查询参数
	 * @param int $timeType 时间类型1下单时间2使用时间3游玩时间(cgp, 2014年1月24日 13:52:18)
	 * @param int $fid   分销商ID
	 * @return array    上面的改写，统计方式不一样
	 */
	public function _orderSum($begintime, $endtime, $by = "lid",
                             $option = array(), $timeType=1, $fid=0,$aid=0)
    {
        $this->init();
        $btime1 = $begintime;
        $etime1 = $endtime;
        $this->params['concat'] = 0;
        if($fid>0) {
            $this->params['mid'] = $fid;
        }
		if($aid>0){
			$this->params['aid'] = $aid;
		}
        switch($timeType) {
            case 2:
                $this->params['btime2'] = $btime1;
                $this->params['etime2'] = $etime1;
                break;
            case 3:
                $this->params['btime3'] = $btime1;
                $this->params['etime3'] = $etime1;
                break;
            default:
                $this->params['btime1'] = $btime1;
                $this->params['etime1'] = $etime1;
                break;
        }
        foreach ($option as $key => $value) {
            $this->params[$key] = $value;
        }
        $simplexml = $this->ordersSimpleXml();
        $result = array();
        $UUby = "UU" . $by;
		// print_r($simplexml);
        foreach ($simplexml->Rec as $row) {
			// print_r($row);
            if ($row->UUpaystatus != 1)
                continue; //未支付订单不统计
			// print_r($row);
			// $result[$arrId] = array();
            $row = $this->secondProOrderHandle($row); //转供应产品价格处理
			// $arrId = substr($);
			if($by!='lid' && $by!='tid'){
				$arrId=md5($row->UUdname);
			}else{
				$arrId=md5((string )$row->{$UUby});
			}
            if ($by == "lid") {
                $result[$arrId]['title'] = (string )$row->UUltitle;
            } elseif ($by == "tid") {
                 $result[$arrId]['title'] = (string )$row->UUltitle . (string )$row->
                    UUttitle;
            } else {
                $result[$arrId]['title'] = (string )$row->UUdname;
				$result[$arrId]['mid'][] = (string )$row->UUmid;
				$result[$arrId]['aid'][] = (string )$row->UUaid;
				$result[$arrId]['aids'][] = (string )$row->UUaids;
				$result[$arrId]['UUtnum'][] = (string )$row->UUtnum;
				$result[$arrId]['UUtotalmoney'][] = (string )$row->UUtotalmoney;
				$result[$arrId]['UUpmode'][] = (string )$row->UUpmode;
				
            }
            $result[$arrId]['order']++;
            $result[$arrId]['tnum'] += $row->UUtnum;
            $result[$arrId]['money'] += (float)$row->UUtnum*(float)$row->UUtprice;
            if ((string )$row->UUpmode == 0)
                $result[$arrId]['pmode0'] += (float)$row->UUtnum * (float)$row->
                    UUtprice;
            if ((string )$row->UUpmode == 1)
                $result[$arrId]['pmode1'] += (float)$row->UUtnum * (float)$row->
                    UUtprice;
            if ((string )$row->UUpmode == 2)
                $result[$arrId]['pmode2'] += (float)$row->UUtnum * (float)$row->
                    UUtprice;
            if ((string )$row->UUpmode == 3)
                $result[$arrId]['pmode3'] += (float)$row->UUtnum * (float)$row->
                    UUtprice;
        }
        return $result;
    }
   /*
    public function ordersCount()
    {
        $params = $this->params;
        extract($params);
        return $this->s->Order_Globle_Search($sid, $mid, $lid, $tid, $ltitle, $ttitle, $btime1,
            $etime1, $btime2, $etime2, $btime3, $etime3, $ordernum, $oname, $otel, $status,
            $pays, $fromt, $orderby, $sort, $rstart, $n, 2, $stype, $payinfo, $pmode, $remotenum,
            $origin, $p_type, $order_confirm, $aid, $concat, $ifpack);

    }*/

    /**
     *订单查询Xml函数.
     *返回调用订单查询接口原始的数据
     *@see ProOrder::orderlist()
     *@see ProOrder::orderSum()
     *@return SimpleXml 订单数据
     */
    public function ordersSimpleXml()
    {
        $params = $this->params;
		// echo $btime3,$etime3;
		// print_r($params);
		// exit;
		//防止集团账号看不见()
		// if($_SESSION['sdtype']==7){
			// $params['aid'] = '';
		// }
        extract($params);
        $xml = $this->s->Order_Globle_Search($sid, $mid, $lid, $tid, $ltitle, $ttitle, $btime1,
            $etime1, $btime2, $etime2, $btime3, $etime3, $ordernum, $oname, $otel, $status,
            $pays, $fromt, $orderby, $sort, $rstart, $n, 0, $stype, $payinfo, $pmode, $remotenum,
            $origin, $p_type, $order_confirm, $aid, $concat, $ifpack);
        $simplexml = simplexml_load_string($xml);

        return $simplexml;
    }

    public function totalOrders()
    {
        $params = $this->params;
        extract($params);
        $ttotalOrders = $this->s->Order_Globle_Search($sid, $mid, $lid, $tid, $ltitle,
            $ttitle, $btime1, $etime1, $btime2, $etime2, $btime3, $etime3, $ordernum, $oname,
            $otel, $status, $pays, $fromt, $orderby, $sort, $rstart, $n, 1, $stype, $payinfo,
            $pmode, $remotenum, $origin, $p_type, $order_confirm, $aid, $concat, $ifpack);
		return $ttotalOrders;

    }
	
}

/**
 *供应商销售订单.
 *会员身份为0供应商
 */
class SupSaleProOrder extends ProOrder
{
    public function __construct($did, $s)
    {
        parent::__construct($did, $s);
        parent::setInitParam("aid", $did);
    }
    public function weekSaleOrders()
    {
        $this->params['ifpack'] = 2;
        return $this->weekOrders();
    }
}
/**
 *供应商购买订单.
 *会员身份为0供应商
 */
class SupBuyProOrder extends ProOrder
{
    public function __construct($did, $s)
    {
        parent::__construct($did, $s);
        parent::setInitParam("mid", $did);
    }
    public function weekSaleOrders()
    {
        return $this->weekOrders();
    }
}

/**
 *分销商购买订单.
 *会员身份为1分销商
 */
class DisBuyProOrder extends ProOrder
{
    public function __construct($did, $s)
    {
        parent::__construct($did, $s);
        parent::setInitParam("mid", $did);
    }
    public function weekSaleOrders()
    {
        return $this->weekOrders();
    }
}


/**
 *资源方销售订单.
 *会员身份为2供应商
 */
class ResourceSaleProOrder extends ProOrder
{
    public function __construct($did, $s)
    {
        parent::__construct($did, $s);
        $str = "select account from pft_member where id=$did";
        $GLOBALS['le']->query($str);
        $GLOBALS['le']->fetch_assoc();
        $sid = $GLOBALS['le']->f("account");
        parent::setInitParam("sid", $sid);
    }
    public function weekSaleOrders()
    {
        $this->params['ifpack'] = 2;
        return $this->weekOrders();
    }
}


/**
 *普通用户购买订单.
 *会员身份为5普通用户
 */
class mobileBuyProOrder extends ProOrder
{
    public function __construct($did, $s)
    {
        parent::__construct($did, $s);
        $str = "select mobile from pft_member where id=$did";
        $GLOBALS['le']->query($str);
        $GLOBALS['le']->fetch_assoc();
        $otel = $GLOBALS['le']->f("mobile");
        parent::setInitParam("otel", $otel);
        parent::setInitParam("stype", 1);
    }
    public function weekSaleOrders()
    {
        return $this->weekOrders();
    }
}


/**
 *集团销售订单.
 *会员身份为7集团账号
 */
class GroupSaleProOrder extends ProOrder
{
    public function __construct($did, $s)
    {
        parent::__construct($did, $s);
        $son_ids = array($did);
        $str = "select son_id from pft_member_relationship where parent_id=$did and son_id_type=3 and ship_type=1";
        $GLOBALS['le']->query($str);
        while ($row = $GLOBALS['le']->fetch_assoc()) {
            $son_ids[] = $row['son_id'];
        }
        parent::setInitParam("aid", implode("|", $son_ids));
    }
    public function weekSaleOrders()
    {
        return $this->weekOrders();
    }
}

/**
 *管理员订单.
 *会员身份为9供应商
 */
class AdminProOrder extends ProOrder
{
    public function __construct($did, $s)
    {
        parent::__construct($did, $s);
    }
    public function weekSaleOrders()
    {
        $this->params['ifpack'] = 2;
        return $this->weekOrders();
    }
}

/**
 *已买到的订单对象函数.
 *由传入会员类型返回对应的订单对象，查询已买到的产品
 *@param int $did 会员ID
 *@param objcet $s 票付通内部接口对象
 *@param int $type 会员类型
 *@return object
 */
function buyProOrderObject($did, $s, $type)
{
    if ($type == 0) {
        return new SupBuyProOrder($did, $s);
    } elseif ($type == 1) {
        return new DisBuyProOrder($did, $s);
    } elseif ($type == 5) {
        return new MobileBuyProOrder($did, $s);
    }elseif ($type == 9) {
        return new AdminProOrder($did, $s);
    }
}

/**
 *已卖出的订单对象函数.
 *由传入会员类型返回对应的订单对象，查询已卖出的产品
 *@param int $did 会员ID
 *@param objcet $s 票付通内部接口对象
 *@param int $type 会员类型
 *@return object
 */
function saleProOrderObject($did, $s, $type)
{
    if ($type == 0) {
        return new SupSaleProOrder($did, $s);
    } elseif ($type == 1) {
        return new DisBuyProOrder($did, $s);
    } elseif ($type == 2) {
        return new ResourceSaleProOrder($did, $s);
    } elseif ($type == 7) {
        return new GroupSaleProOrder($did, $s);
    } elseif ($type == 9) {
        return new AdminProOrder($did, $s);
    }
}
?>